package com.eleadmin.common.system.controller;

import cn.hutool.core.date.DateUtil;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.system.service.OrderService;
import com.eleadmin.common.system.entity.Order;
import com.eleadmin.common.system.param.OrderParam;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.BatchParam;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 订单表控制器
 *
 * @author EleAdmin
 * @since 2025-04-22 22:55:28
 */
@Api(tags = "订单表管理")
@RestController
@RequestMapping("/system/order")
public class OrderController extends BaseController {
    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @OperationLog
    @ApiOperation("分页查询订单表")
    @GetMapping("/page")
    public ApiResult<PageResult<Order>> page(OrderParam param) {

        PageResult<Order> orderPageResult = orderService.pageRel(param);
        orderPageResult.getList().forEach(order -> {
            order.setUserName(userService.getById(order.getUserId()).getNickname());

        });

        return success(orderPageResult);

    }


    @OperationLog
    @ApiOperation("查询全部订单表")
    @GetMapping()
    public ApiResult<List<Order>> list(OrderParam param) {



        List<Order> orderList = orderService.listRel(param);


        return success(orderList);

    }


    @OperationLog
    @ApiOperation("根据id查询订单表")
    @GetMapping("/{id}")
    public ApiResult<Order> get(@PathVariable("id") Integer id) {
        Order order = orderService.getById(id);
        Integer acceptId = order.getAcceptId();
        if (acceptId != null) {
            order.setAcceptUser(userService.getById(acceptId));
        }
        return success(order);

    }


    @OperationLog
    @ApiOperation("添加订单表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Order order) {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric = RandomStringUtils.randomNumeric(3);
        String orderNum = localDate + randomNumeric;
        order.setOrderNo(orderNum);
        order.setUserId(getLoginUserId());
        order.setStatus("待接单");
        if (orderService.save(order)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }
    


    @OperationLog
    @ApiOperation("修改订单表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Order order) {
        if (orderService.updateById(order)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @ApiOperation("修改订单状态")
    @PutMapping("/updateOrderStatus")
    public ApiResult<?> updateOrderStatus(@RequestBody Order order) {
        String status = order.getStatus();
        if (status.equals("待送达")) {//说明已经接单了
            order.setAcceptId(getLoginUserId());
            order.setAcceptTime(DateUtil.date());
        }
        if (orderService.updateById(order)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @OperationLog
    @ApiOperation("删除订单表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }


    @OperationLog
    @ApiOperation("批量添加订单表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Order> list) {
        if (orderService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @OperationLog
    @ApiOperation("批量修改订单表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Order> batchParam) {
        if (batchParam.update(orderService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @OperationLog
    @ApiOperation("批量删除订单表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
